
function res = trebufunV2(theta,phi,t,R,L1, L2,M1,M2,g)
phi = (phi/180)*pi;
theta = (theta/180) *pi;
trebvars = [theta;phi;0;0] ;
options = odeset('Events', @events);

    function [value,isterminal,direction] = events(t,trebvars)

       value= mod(trebvars(1),2*pi) - mod(trebvars(2),2*pi)+pi/6;
       value=sin(value+pi);
        isterminal = 1;
        direction = -1;
    end
[T,K] = ode45(@(t,trebvars)acceleration(trebvars,L1,L2,M1,M2,R,g),[0,t],trebvars,options);
res = [T,K];
end

function acc=acceleration(trebvars,L1,L2,M1,M2,R,g)
 theta = trebvars(1);
phi = trebvars(2);
thetadot = trebvars(3);
phidot = trebvars(4);
leftover = [M1*g*R*cos(theta)-M2*g*L1*cos(theta)-M2*L1*L2*(phidot^2)*sin(theta-phi);
           -M2*g*L2*cos(phi)+ M2*L1*L2*(thetadot^2)*sin(theta-phi)]; 
        %right side of matrix equation, top is theta
        
dotstuff = [(M1*R^2+M2*L1^2),M2*L1*L2*cos(theta-phi);
             L1*L2*M2*cos(theta-phi),M2*L2^2 ] ;             
         %left side of matrix equation, top is theta
      
doubledot = dotstuff\leftover; 
%calulates theta and phi umblat, in that order
    

acc = [thetadot;phidot;doubledot(1);doubledot(2)];
end
